using System;
using System.Drawing;
using System.Data;
using System.Data.Common;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;

namespace Tilver.DBConnectionUI.MySQL
{
    [ToolboxItem(false)]
    internal partial class MySQLEditor :UserControl, Tilver.DBConnectionUI.IUDLEditor, IOleDbEditor
	{
        MySQLStringBuilder _builder = new MySQLStringBuilder();

        public MySQLEditor()
        {
            InitializeComponent();

            txServer.TextChanged += delegate { _builder.DataSource = txServer.Text; };
            txPort.TextChanged += delegate { _builder.Port = Convert.ToInt32(txPort.Text); };
            txUserID.TextChanged += delegate { _builder.UserID = txUserID.Text; };
            txPassword.TextChanged += delegate { _builder.Password = txPassword.Text; };
            txInitialCatalog.TextChanged += delegate { _builder.InitialCatalog = txInitialCatalog.Text; };
            
            txPort.Validating += new CancelEventHandler(ValidateInt32);
        }

        void ValidateInt32(object sender, CancelEventArgs e)
        {
            if (sender is TextBoxBase)
            {
                TextBoxBase txt = (TextBoxBase)sender;
                try
                {
                    Convert.ToInt32(txt.Text);
                    txt.ForeColor = SystemColors.ControlText;
                    e.Cancel = false;
                }
                catch (Exception) 
                {
                    txt.ForeColor = Color.Red;
                    e.Cancel = true; 
                }
            }
        }

        #region IConnectionEditor Members

        public string ConnectionString 
        { 
            get 
            {
                if (string.IsNullOrEmpty(oleprov))
                    return _builder.ConnectionString;
                else
                    return string.Format("Provider={0};{1}", oleprov, _builder.ConnectionString);
            
            }
            set 
            { 
                _builder.ConnectionString = value;
                RefreshData();
            }
        }

        public void RefreshData()
        {
            txServer.Text = _builder.DataSource;
            txPort.Text = Convert.ToString(_builder.Port);
            txUserID.Text = _builder.UserID;
            txPassword.Text = _builder.Password;
            txInitialCatalog.Text = _builder.InitialCatalog;

        }

        public object GetAdvancedPropertiesObject()
        {
            return _builder;
        }

        #endregion



        #region IOleDbEditor Members

        string oleprov;
        public void SetOleProviderID(string invariantName)
        {
            oleprov = invariantName;
            _builder.AddKeywordOverride("Data Source", "Server");
            _builder.AddKeywordOverride("Initial Catalog", "Database");
            _builder.AddKeywordOverride("User ID", "UID");
            _builder.AddKeywordOverride("Password", "PWD");
        }

        #endregion
    }
}
